﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Input;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.XJigLib;
using PloobsEngine.Light;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Commands;
using PloobsEngineDemo3;
using PloobsEngine.Draw;

namespace Etapa1.Screens
{    
    /// <summary>
    /// Camera Path Screen
    /// Record, Load Path, Camera Types
    /// </summary>
    public class CameraPathScreen : SceneScreen
    {
        private JigLibxWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        CameraRecordPath record;
        
        public CameraPathScreen(EngineStuff es)
        {
            this.engine = es;
            engine.BackGroundColor = Color.Sienna;
            physicWorld = new JigLibxWorld();            
            mundo = new BrutalForceWorld(physicWorld);            
        }

        public override void LoadContent(EngineStuff es)
        {
            InputAdvanced ia = new InputAdvanced(engine);
            engine.AddComponent(ia);
           
            base.LoadContent(es);


            ///Cenario de sempre
            SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario" );
            sm.LoadModelo();            
            IShader normal = new NormalDeferred();
            normal.Initialize();
            IMaterial mat = new Material(normal); 
            IPhysicObject pi = new TriangleMeshObject(sm, Matrix.Identity, new Vector3(0,10,100));            
            pi.isMotionLess = true;
            IObject obj4 = new SimpleObject(mat, pi, sm);
            mundo.AddObject(obj4);
                        

            ///Luzes
            DirectionalLight ld = new DirectionalLight(new Vector3(4, -2, 7), Color.White);
            //LightDirectional ld2 = new LightDirectional(new Vector3(-1, 0, -1), Color.White);
            //LightDirectional ld3 = new LightDirectional(new Vector3(0, 0, 1), Color.White);
            //LightDirectional ld4 = new LightDirectional(new Vector3(1, 0, 0), Color.White);
            mundo.AddLight(ld);                       
            //mundo.AddLight(ld2);
            //mundo.AddLight(ld3);
            //mundo.AddLight(ld4);
                       
           

            ICamera cam = new CameraFirstPerson();
            cam.Name = "stdCam";
            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };            
            this.RenderTechnics = rt;                       
            mundo.AddCamera(cam);                                                        
            this.World = mundo;

            {
                InputPlayableKeyBoard ipp = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.R, Start, EntityType.TOOLS);
                BindKeyCommand bkk = new BindKeyCommand(ipp, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bkk);

                InputPlayableKeyBoard ipp2 = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.T, Stop, EntityType.TOOLS);
                BindKeyCommand bkk2 = new BindKeyCommand(ipp2, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bkk2);

                InputPlayableKeyBoard ipp3 = new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.Y, Load, EntityType.TOOLS);
                BindKeyCommand bkk3 = new BindKeyCommand(ipp3, BindAction.ADD);
                CommandProcessor.getCommandProcessor().SendCommandAssyncronous(bkk3);
            }

            ///Camera Recorder            
            record = new CameraRecordPath(engine, cam);
            record.FinishInTheStartPosition = true;
            

            Drawing.ClearEveryFrame = false;
            
        }

        /// <summary>
        /// r
        /// </summary>
        /// <param name="ipk"></param>
        private void Start(InputPlayableKeyBoard ipk)
        {
            record.StartRecord();

            Drawing.ClearAll();
            Drawing.Draw2dTextAt2dLocation("Recording", new Vector2(20, 20), Color.White);
        }

        /// <summary>
        /// y
        /// </summary>
        /// <param name="ipk"></param>
        private void Load(InputPlayableKeyBoard ipk)
        {
            CameraPathData pd = record.LoadCurveFile("teste.bin");
            CameraFollowPath fcp = new CameraFollowPath(pd, mundo, "stdCam");
            fcp.OnLoop = true;
            mundo.AddCamera("follow", fcp);
            mundo.setActiveCamera("follow");

            Drawing.ClearAll();
            Drawing.Draw2dTextAt2dLocation("Loading - Following", new Vector2(20, 20), Color.White);
        }

        /// <summary>
        /// t
        /// </summary>
        /// <param name="ipk"></param>
        private void Stop(InputPlayableKeyBoard ipk)
        {
            record.StopRecord();
            record.SaveCurveToFile("teste.bin");

            Drawing.ClearAll();
            Drawing.Draw2dTextAt2dLocation("Stopped - Saved", new Vector2(20, 20), Color.White);
        }


    }
}
